package com.xingchi.tornado.storage.core.model;

import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;

/**
 * 文件上传DTO
 *
 * @author xingchi
 * @date 2024/4/10 20:34
 */
@Data
@EqualsAndHashCode
public class FileUploadWrapper {

    /**
     * 文件名称
     */
    private String fileName;

    /**
     * 文件流
     */
    private InputStream inputStream;

    /**
     * 临时文件名称
     */
    private String tempFileName;

    /**
     * 文件字节
     */
    private byte[] bytes;

    /**
     * 用户id
     */
    private Long userId;

    public boolean isEmpty() {
        return this.inputStream == null && this.bytes == null;
    }

    public static FileUploadWrapper wrap(MultipartFile file) {

        try {
            FileUploadWrapper context = new FileUploadWrapper();
            InputStream inputStream = file.getInputStream();
            context.setFileName(file.getOriginalFilename());
            context.setInputStream(inputStream);
            return context;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    public static FileUploadWrapper wrap(File file) {
        try {
            FileUploadWrapper context = new FileUploadWrapper();

            if (!file.isFile()) {
                throw new IOException(String.format("%s 不是一个文件", file.getAbsoluteFile()));
            }

            InputStream is = Files.newInputStream(file.toPath());
            context.setFileName(file.getName());
            context.setInputStream(is);
            return context;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static FileUploadWrapper wrap(File file, String rename) {
        try {
            FileUploadWrapper context = new FileUploadWrapper();

            if (!file.isFile()) {
                throw new IOException(String.format("%s 不是一个文件", file.getAbsoluteFile()));
            }

            InputStream is = Files.newInputStream(file.toPath());
            context.setFileName(StringUtils.isBlank(rename) ? file.getName() : rename);
            context.setInputStream(is);
            return context;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static FileUploadWrapper wrap(InputStream inputStream, String fileName) {
        FileUploadWrapper context = new FileUploadWrapper();
        context.setInputStream(inputStream);
        context.setFileName(fileName);
        return context;

    }

    public static FileUploadWrapper wrap(byte[] bytes, String fileName) {
        FileUploadWrapper context = new FileUploadWrapper();
        context.setFileName(fileName);
        context.setBytes(bytes);
        return context;
    }

    public byte[] getBytes() {
        if (bytes == null) {
            if (inputStream != null) {
                try {
                    bytes = inputStream.readAllBytes();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return bytes;
    }

    public InputStream getInputStream() {

        if (inputStream == null) {
            if (bytes != null) {
                return inputStream = new ByteArrayInputStream(getBytes());
            }
        }

        return inputStream;
    }
}
